package com.aisile.crowd_funding.system.service.impl;

import com.aisile.crowd_funding.system.mapper.SysRoleMapper;
import com.aisile.crowd_funding.system.mapper.SysRoleMenuMapper;
import com.aisile.crowd_funding.system.mapper.SysUserRoleMapper;
import com.aisile.crowd_funding.system.pojo.*;
import com.aisile.crowd_funding.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @标题 RoleServiceImpl
 * @作者 Mr.chen
 * @创建日期 2018/12/10 14:36
 * @版本号 1.0
 **/
@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    SysRoleMapper sysRoleMapper;
    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;
    @Autowired
    SysUserRoleMapper sysUserRoleMapper;


    @Override
    public int deleteByPrimaryKey(Long id) {
        int i = sysRoleMapper.deleteByPrimaryKey(id);
        SysRoleMenuExample sysRoleMenuExample = new SysRoleMenuExample();
        sysRoleMenuExample.createCriteria().andRoleIdEqualTo(id);
        sysRoleMenuMapper.deleteByExample(sysRoleMenuExample);
        SysUserRoleExample sysUserRoleExample = new SysUserRoleExample();
        sysUserRoleExample.createCriteria().andRoleIdEqualTo(id);
        sysUserRoleMapper.deleteByExample(sysUserRoleExample);
        return i;
    }
    @Override
    public int insert(SysRole record) {
        int insert = sysRoleMapper.insert(record);
        Long roleId = record.getRoleId();
        Long[] menuIds = record.getMenuIds();
        //把  menuIds 和 插入成功的 roleId   放入  role_menu表当中
        for(Long menuId : menuIds){
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenuMapper.insert(sysRoleMenu);
        }
        return insert;
    }
    @Override
    public SysRole selectByPrimaryKey(Long id) {
        return sysRoleMapper.selectByPrimaryKey(id);
    }
    @Override
    public int updateByPrimaryKey(SysRole sysRole) {
        int i = sysRoleMapper.updateByPrimaryKey(sysRole);
        SysRoleMenuExample example = new SysRoleMenuExample();
        example.createCriteria().andRoleIdEqualTo(sysRole.getRoleId());
        sysRoleMenuMapper.deleteByExample(example);
        //把  menuIds 和 插入成功的 roleId   放入  role_menu表当中
        Long[] menuIds = sysRole.getMenuIds();
        for(Long menuId : menuIds){
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setRoleId(sysRole.getRoleId());
            sysRoleMenuMapper.insert(sysRoleMenu);
        }
        return i;
    }
    @Override
    public List<SysRole> selectByExample(SysRoleExample sysRoleExample) {
        return sysRoleMapper.selectByExample(sysRoleExample);
    }

    @Override
    public void deleteByIds(Long[] ids) {
        for(Long id : ids){
            deleteByPrimaryKey(id);
        }
    }
}
